<?php
namespace app\admin\service;
use think\Db;
class  Front{


    //文书类型
    function frontTypeList(){
        $paginate = 15;
        $info  = Db::name('writ_type')->order('wt_sort desc,wt_addtime desc')->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }


    //文书类型新增与编辑
    function writTyupeRequestApi($wt_name,$wt_sort,$writ_type_id){
        if(empty($wt_name)){
            return messageInfo('101','分类名必需填写');
        }
        if(!is_numeric($wt_sort)){
            return messageInfo('101','排序必需填写数字');
        }
        $data_array['wt_sort'] = $wt_sort;
        $data_array['wt_name'] = $wt_name;
        $writ_type = Db::name('writ_type');
        if(empty($writ_type_id)){
            $data_array['wt_addtime']   = date('Y-m-d H:i:s');
            $a = $writ_type->insert($data_array);
            $return_name = '新增';
        }else{
            $data_array['writ_type_id'] = $writ_type_id;
            $a = $writ_type->update($data_array);
            $return_name = '编辑';
        }
        if($a){
            $response = messageInfo('202',$return_name.'成功');
        }else{
            $response = messageInfo('101',$return_name.'失败');
        }
        return $response;
    }

    //查询文书类型
    function writTypeFind($writ_type_id){
        $data = Db::name('writ_type')->where('writ_type_id',$writ_type_id)->find();
        if(empty($data)){
            return messageInfo('101','数据查询失败');
        }
        return messageInfo('202','数据查询成功',$data);
    }

    //文书分类删除
    function writTypeDeleteApi($writ_type_id){
        if(empty($writ_type_id)){
            return messageInfo('101','系统参数错误');
        }
        $a = Db::name('writ_type')->where('writ_type_id',$writ_type_id)->delete();
        if(!$a){
            return messageInfo('101','删除失败');
        }
        return messageInfo('202','删除成功');
    }


    //法律文书
    function writList(){
        $join = [
            ['lt_writ_type b','a.writ_type_id=b.writ_type_id'],
        ];
        $field    = 'a.*,b.wt_name';
        $paginate = 15;
        $list     = Db::name('writ')->alias('a')->join($join)->field($field)->order('a.w_addtime desc')->paginate($paginate);
        $page     = $list->render();
        $data['list'] = $list;
        $data['page'] = $page;
        return $data;
    }

    //法律类型查询
    function writType(){
        $list = Db::name('writ_type')->field('writ_type_id,wt_name')->select();
        return $list;
    }


    //法律文书新增与编辑
    function writPresentApi($parr,$file=''){

        if(empty($parr['writ_id'] ) && empty($file)){
            return messageInfo('101','新增必需上传文书文档！');
        }
        if(empty($parr['w_title']) || empty($parr['w_describe'])){
            return messageInfo('101','文书标题与文书描述必需填写！');
        }
      /*  if(empty($parr['w_writ_img'])){
            return messageInfo('101','显示图必需填写！');
        }*/
        if(empty($parr['w_industry']) || empty($parr['w_copyright'])){
            return messageInfo('101','行业与版权必需填写！');
        }
        $data_array = array(
            'w_title'       => $parr['w_title'],
            'writ_type_id'  => $parr['writ_type_id'],
            'w_describe'    => $parr['w_describe'],
            'w_state'       => $parr['w_state'],
            'w_industry'    => $parr['w_industry'],
            'w_copyright'   => $parr['w_copyright'],
            'w_sort'        => $parr['w_sort']?$parr['w_sort']:0,
            /*'w_writ_img'    => $parr['w_writ_img'],*/
        );
        $writ = Db::name('writ');
        try {
            if(empty($parr['writ_id'])){ //新增
                $wordUp = $this->wordUp($file);
                if($wordUp['code'] == '101'){
                    return $wordUp;
                }
                $data_array['w_writ_url'] = $wordUp['data'];
                $data_array['w_addtime']  = date('Y-m-d H:i:s');
                $a = $writ->insert($data_array);
                $return_name = '新增';
            }else{//编辑
                if(!empty($file)){
                    $wordUp = $this->wordUp($file);
                    if($wordUp['code'] == '101'){
                        return $wordUp;
                    }
                    $data_array['w_writ_url'] = $wordUp['data'];
                }
                    $data_array['writ_id']    = $parr['writ_id'];
                $a = $writ->update($data_array);
                $return_name = '编辑';
            }
            if($a){
                $url = config('tempredirect_url').'front/writ_list';
                $response = messageInfo('202',$return_name.'成功','',$url);
            }else{
                $response = messageInfo('101',$return_name.'失败');
            }
        } catch (\Exception  $e) {
            $response = messageInfo('101','服务器错误');
        }
        return $response;
    }


    //文书文档上传
    function wordUp($file){
        $move = ROOT_PATH . 'public' . DS . 'uploads'. DS .'writword';
        $info =$file->validate(['ext'=>'doc,docx'])->move($move);
        if($info){
            $url     = 'uploads'. DS .'writword'. DS .$info->getSaveName();
            $response = messageInfo('202','成功',$url);
        }else{
            // 上传失败获取错误信息
            $error    = $file->getError();
            $response = messageInfo('101', $error);
        }
        return $response;
    }

    //文书详情
    function writFind(){
        $writ_id = input('writ_id');
        $data    = Db::name('writ')->where('writ_id',$writ_id)->find();
        return $data;
    }

    //文书删除
    function writDeleteApi($writ_id){
        if(!$writ_id){
            return messageInfo('101','系统参数错误');
        }
        $writ = Db::name('writ');
        $where['writ_id'] = $writ_id;
    /*    $find = $writ->where($where)->field('w_writ_img,w_writ_url')->find();*/
        $a    = $writ->where($where)->delete();
        if($a){
            $response = messageInfo('202','删除成功');
        }else{
            $response = messageInfo('101','删除失败');
        }
        return $response;
    }



    //文书发布与取消发布
    function writAnnounceApi($writ_id){
        if(empty($writ_id)){
            return messageInfo('101','系统参数错误');
        }
        $writ = Db::name('writ');
        $where['writ_id'] = $writ_id;
        $w_state = $writ->where($where)->value('w_state');
        $value   = $w_state==1?2:1;
        $a       = $writ->where($where)->setField('w_state',$value);
        if(!$a){
            return messageInfo('101','操作失败');
        }
        return messageInfo('202','操作成功');
    }

    //律师展示
    function lawyerList(){
        $paginate = 15;
        $info  = Db::name('lawyer')->order('l_addtime desc')->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }

    //律师展示新增与编辑
    function lawyerPresentApi($parr,$lawyer_id){
        if(empty($parr['l_name']) || empty($parr['l_office']) || empty($parr['l_describe'])){
            return messageInfo('101','律师姓名、所属事务所、描述必需填写！');
        }
        if(empty($parr['l_site'])){
            return messageInfo('101','所在地址必需填写！');
        }
        if(empty($parr['l_code'])){
            return messageInfo('101','执业证号必需填写！');
        }
        if(empty($parr['l_field'])){
            return messageInfo('101','业务领域必需填写！');
        }
        if(empty($parr['l_img'])){
            return messageInfo('101','律师头像必需必需上传！');
        }
        $lawyer = Db::name('lawyer');
        if(empty($lawyer_id)){
            $parr['l_addtime'] = date('Y-m-d H:i:s');
            $a                 = $lawyer->insert($parr);
            $return_name       = '新增';
        }else{
            $parr['lawyer_id'] = $lawyer_id;
            $a  = $lawyer->update($parr);
            $return_name       = '编辑';
        }
        if($a){
            $url = config('tempredirect_url').'front/lawyer_list';
            $response = messageInfo('202',$return_name.'成功','',$url);
        }else{
            $response = messageInfo('101',$return_name.'失败');
        }
        return $response;
    }

    //律师展示详情
    function lawyerFind($lawyer_id){
        $data = Db::name('lawyer')->where('lawyer_id',$lawyer_id)->find();
        return $data;
    }

    //律师删除
    function lawyerDeletApi($lawyer_id){
        if(!$lawyer_id){
            return messageInfo('101','系统参数错误');
        }
        $a    = Db::name('lawyer')->where('lawyer_id',$lawyer_id)->delete();
        if($a){
            $response = messageInfo('202','删除成功');
        }else{
            $response = messageInfo('101','删除失败');
        }
        return $response;
    }


    //咨询类型列表
    function newsTypeList(){
        $paginate = 15;
        $info  = Db::name('news_type')->order('nt_sort desc,nt_addtims desc')->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }

    //咨讯类型新增与编辑
    function  newsTypeRequestApi($nt_name,$nt_sort,$news_type_id){
        if(empty($nt_name)){
            return messageInfo('101','分类名必需填写');
        }
        if(!is_numeric($nt_sort)){
            return messageInfo('101','排序必需填写数字');
        }
        $data_array['nt_name'] = $nt_name;
        $data_array['nt_sort'] = $nt_sort;
        $news_type = Db::name('news_type');
        if(empty($news_type_id)){
            $data_array['nt_addtims']   = date('Y-m-d H:i:s');
            $a = $news_type->insert($data_array);
            $return_name = '新增';
        }else{
            $data_array['news_type_id'] = $news_type_id;
            $a = $news_type->update($data_array);
            $return_name = '编辑';
        }
        if($a){
            $response = messageInfo('202',$return_name.'成功');
        }else{
            $response = messageInfo('101',$return_name.'失败');
        }
        return $response;
    }

    //咨讯类型查询
    function newsTypeFind(){
        $news_type_id = input('news_type_id');
        $data         = Db::name('news_type')->where('news_type_id',$news_type_id)->find();
        if(empty($data)){
            return messageInfo('101','数据查询失败');
        }
        return messageInfo('202','数据查询成功',$data);
        return $data;
    }

    //咨讯类型删除
    function newsTypeDeleteApi($news_type_id){
        if(empty($news_type_id)){
            return messageInfo('101','系统参数错误');
        }
        $a = Db::name('news_type')->where('news_type_id',$news_type_id)->delete();
        if(!$a){
            return messageInfo('101','删除失败');
        }
        return messageInfo('202','删除成功');
    }


    //资讯列表
    function newsList(){
        $join = [
            ['lt_news_type b','a.news_type_id=b.news_type_id'],
        ];
        $field    = 'a.*,b.nt_name';
        $paginate = 15;
        $list     = Db::name('news')->alias('a')->join($join)->field($field)->order('a.n_newstime desc,a.n_addtime desc')->paginate($paginate);
        $page     = $list->render();
        $data['list'] = $list;
        $data['page'] = $page;
        return $data;
    }

    //咨讯类型查询
    function newsTypeSelect(){
        $list = Db::name('news_type')->field('news_type_id,nt_name')->order('nt_sort desc,nt_addtims desc')->cache(120)->select();
        return $list;
    }

    //资讯新增与编辑
    function newsAnyRequestApi($parr){
        try{
            if(empty($parr['n_title']) || empty($parr['n_describe'])){
             return   messageInfo('101','资讯标题与描述必需填写！');
            }
            if(empty($parr['n_newstime']) || empty($parr['n_source'])){
                return   messageInfo('101','资讯时间与来源必需填写！');
            }
            if(empty($parr['n_img'])){
                return   messageInfo('101','资讯咨讯显示图必需填写！');
            }
            $news = Db::name('news');
            if(empty($parr['news_id'])){
                $parr['n_addtime'] = date('Y-m-d H:i:s');
                $a = $news->insert($parr);
            }else{//编辑
                $a = $news->update($parr);
            }
            if(!$a){
                return messageInfo('101','提交失败!');
            }
            $url = config('tempredirect_url').'front/news_list';
            return messageInfo('202','提交成功!','',$url);
        }catch(\Exception  $e){
            $response = messageInfo('101','服务器错误');
        }
        return $response;
    }

    //资讯删除
    function newsDeleteApi($news_id){
        if(!$news_id){
            return   messageInfo('101','系统参数错误！');
        }
        $a = Db::name('news')->where('news_id',$news_id)->delete();
        if(!$a){
            return messageInfo('101','删除失败!');
        }
        return messageInfo('202','删除成功!');
    }

    //资讯发布操作
    function newsIssueApi($news_id){
        if(!$news_id){
            return   messageInfo('101','系统参数错误！');
        }
        $news = Db::name('news');
        $n_state = $news->where('news_id',$news_id)->value('n_state');
        if($n_state == 1){
            $value = 2;
        }else{
            $value = 1;
        }
        $a = Db::name('news')->where('news_id',$news_id)->setField('n_state',$value);
        if(!$a){
            return messageInfo('101','操作失败!');
        }
        return messageInfo('202','操作成功!');
    }

    //资讯查询
    function newsFind($news_id){
        $data = Db::name('news')->where('news_id',$news_id)->find();
        return $data;
    }


    //顾问卡
    function counselorList(){
        $paginate = 15;
        $list     = Db::name('counselor')->order('ck_sort desc,ck_addtime desc')->paginate($paginate);
        $info     = $list->toArray();
        $page     = $list->render();
        foreach($info['data'] as $k=>$v){
             $ps_list = explode('&',$v['ck_ps']);
             $info['data'][$k]['ps_list'] = $ps_list;
        }
        $data['list'] = $info['data'];
        $data['page'] = $page;
        return $data;
    }

    //顾问卡详情
    function counselorFind($counselor_id){
        $data = Db::name('counselor')->where('counselor_id',$counselor_id)->find();
        $data['ps_list']  = explode('&',$data['ck_ps']);
        return $data;
    }

    //顾问卡新增与编辑
    function counselorAnyRequestpi($parr){
        if(empty($parr['ck_name'])){
               return messageInfo('101','顾问卡名称必需填写!');
        }
        if(!is_numeric($parr['ck_price']) || !is_numeric($parr['ck_sort'])){
            return messageInfo('101','价格与排序必需填写整数!');
        }
        $ps_array = array_filter($parr['ck_ps']);
        if(empty($ps_array)){
            return messageInfo('101','内容必需填写6个!');
        }
        $ck_ps      = implode('&',$ps_array);
        $data_array = array(
            'ck_name'   => $parr['ck_name'],
            'ck_price'  => $parr['ck_price'],
            'ck_sort'   => $parr['ck_sort'],
            'ck_ps'     => $ck_ps,
        );
        $counselor  = Db::name('counselor');
        if($parr['counselor_id']){
            $data_array['counselor_id'] = $parr['counselor_id'];
            $a = $counselor->update($data_array);
        }else{
            $data_array['ck_addtime']   = date('Y-m-d H:i:s');
            $a = $counselor->insert($data_array);
        }
        if(!$a){
            return messageInfo('101','提交失败!');
        }
        $url = config('tempredirect_url').'front/counselor_list';
        return messageInfo('202','提交成功!','',$url);
    }


    //顾问卡删除
    function counselorDeleteApi($counselor_id){
        if(empty($counselor_id)){
            return messageInfo('101','系统参数错误!');
        }
        $a = Db::name('counselor')->where('counselor_id',$counselor_id)->delete();
        if(!$a){
            return messageInfo('101','删除失败!');
        }
        return messageInfo('202','操作成功!');
    }

    //查询轮播图
    function slideshowList(){
        $data = Db::name('carousel')->select();
        return $data;
    }


    //轮播图上传
    function carouselImgApi($carousel_img,$carousel_url){
        try{
            $carousel = Db::name('carousel');
            $data     = $carousel->select();
            $carousel->startTrans();
            if (!empty ($data)) {
                foreach ($data as $k => $v) {
                    $where_a ['carousel_id'] = $v ['carousel_id'];
                    $b = $carousel->where($where_a)->delete();
                    if ($b) {
                        $str [] = 1;
                    } else {
                        $carousel->startTrans();
                        $str [] = 2;
                    }
                }
                if (in_array(2, $str)) {
                    $response = messageInfo('101', '编辑失败');
                    return $response;
                }
            }
            $i   = 0;
            $arr = array();
            $carousel_img = array_filter($carousel_img);
            foreach ($carousel_img as $k1 => $v1) {
                if (!empty ($v1)) {
                    $i++;
                    $arr [$k1] ['carousel_img'] = $v1;
                }
            }
            if(empty($arr)){
                $carousel->commit();
                return messageInfo('202', '编辑成功');
            }
            foreach ($carousel_url as $k => $v) {
                if (!empty ($v)) {
                    $find = 'http://';
                    $hp = strpos($v, $find);
                    $find_1 = 'https://';
                    $hp_1 = strpos($v, $find_1);
                    foreach ($arr as $ks => $vs) {
                        if ($ks == $k) {
                            if ($hp === false) {
                                $arr [$k] ['carousel_url'] = $find . $v;
                            } else if ($hp_1 === false) {
                                $arr [$k] ['carousel_url'] = $hp_1 . $v;
                            } else {
                                $arr [$k] ['carousel_url'] = $v;
                            }
                        }
                    }
                } else {
                    foreach ($arr as $ks => $vs) {
                        if ($ks == $k) {
                            $arr [$k] ['carousel_url'] = null;
                        }
                    }
                }
            }
            $b_1 = Db::table('lt_carousel')->insertAll($arr);
            if ($b_1) {
                $carousel->commit();
                $response = messageInfo('202', '编辑成功');
            } else {
                $carousel->startTrans();
                $response = messageInfo('101', '编辑失败');
            }
        }catch(\Exception  $e){
            $response = messageInfo('101','服务器错误');
        }
        return $response;
    }



}
